package org.dant.ant.extension.tasks;

import java.io.*;
import java.util.*;

import org.apache.tools.ant.types.Commandline;
import org.dant.ant.extension.tasks.commands.ExecCommand;
import org.dant.ant.extension.tasks.internal.Endpoint;
import org.dant.ant.extension.tasks.internal.RemoteCommand;

public class RemoteExec extends RemoteExecutable {

    protected Commandline cmdl = new Commandline();
    protected String executableString;

    public RemoteExec() {
        rcmd = new ExecCommand();
    }
  
    public void setExecutable(String value) {
        executableString = value;
        cmdl.setExecutable(value);
    }

    public String getExecutable() {
        return cmdl.getExecutable();
    }

    public Commandline.Argument createArg() {
        return cmdl.createArgument();
    }

    public String[] getArguments() {
        return cmdl.getArguments();
    }

    public void resolveDir() throws IOException {
        for (Iterator iter = destHosts.keySet().iterator(); iter.hasNext();) {
            Endpoint ep = (Endpoint) iter.next();
            RemoteCommand request = (RemoteCommand)destHosts.get(ep);
            File basedir = ep.getBaseDir();
            File execdir = (dirstring == null) ? null : new File(dirstring);
            if (basedir == null || (execdir != null && execdir.isAbsolute())) {
                continue;
            }
            File absolutePath = fu.resolveFile(basedir, dirstring);
            request.appendParameters("dir", absolutePath);
      
            if ((new File(executableString)).isAbsolute()) {
                continue;
            }
            File absoluteExec = fu.resolveFile(basedir, executableString);
            if (absoluteExec != null) {
                request.appendParameters("executable", absoluteExec.getCanonicalPath());
            }
        }
    }

}
